package com.own.business.system.menu.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.system.menu.business.entity.form.SystemMenuAdvancedSortForm;
import com.own.business.system.menu.business.service.SystemMenuService;
import com.own.business.system.menu.entity.bo.SystemMenuBo;
import com.own.business.system.menu.entity.map.SystemMenuMapVo;
import com.own.business.system.menu.entity.po.SystemMenu;
import com.own.business.system.menu.entity.query.SystemMenuQuery;
import com.own.business.system.menu.entity.vo.SystemMenuVo;
import com.own.business.system.menu.mapper.SystemMenuMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.store.core.util.PermissionCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 * SystemMenuServiceImpl
 *
 * @author chenxueli
 * @date 2024-03-31 09:22:58
 */
@Slf4j
@Service
public class SystemMenuServiceImpl extends AbstractBaseService<
        SystemMenu,
        SystemMenuBo,
        SystemMenuVo,
        SystemMenuMapVo,
        SystemMenuQuery,
        SystemMenuMapper
        > implements SystemMenuService {

    @Resource
    private SystemMenuMapper systemMenuMapper;

    @Resource
    private PermissionCacheUtil permissionCacheUtil;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<SystemMenu> lambdaQueryWrapper(SystemMenuQuery query) {
        return super.lambdaQueryWrapper(query)
                .like(StringUtils.isNotBlank(query.getCode()), SystemMenu::getCode, query.getCode())
                .like(StringUtils.isNotBlank(query.getTitle()), SystemMenu::getTitle, query.getTitle())
                .eq(!Objects.isNull(query.getParentId()), SystemMenu::getParentId, query.getParentId())
                .eq(!Objects.isNull(query.getIsDisable()), SystemMenu::getIsDisable, query.getIsDisable())
                .eq(!Objects.isNull(query.getIsShow()), SystemMenu::getIsShow, query.getIsShow())
                .eq(StringUtils.isNotBlank(query.getClientId()), SystemMenu::getClientId, query.getClientId())
                .orderByDesc(SystemMenu::getGmtCreate, SystemMenu::getId);
    }

    /**
     * 根据客户端id查询菜单列表
     *
     * @param clientId  客户端id
     * @param isDisable 是否禁用
     * @param isShow    是否显示
     * @return 菜单列表
     */
    @Override
    public List<SystemMenu> listByClientId(String clientId, Boolean isDisable, Boolean isShow) {
        var wrapper = lambdaQueryWrapper().eq(SystemMenu::getClientId, clientId);
        if (isDisable != null) {
            wrapper.eq(SystemMenu::getIsDisable, isDisable ? 1 : 0);
        }
        if (isShow != null) {
            wrapper.eq(SystemMenu::getIsShow, isShow ? 1 : 0);
        }
        return listByWrapper(wrapper);
    }

    /**
     * 验证po对象
     *
     * @return 验证后的po对象
     */
    @Override
    public Function<SystemMenuVo, SystemMenuVo> checkAfter() {
        return vo -> {
            // 更新完成之后设置权限的全局信息
            permissionCacheUtil.setGlobalTimestamp();
            return vo;
        };
    }

    /**
     * 删除数据之后
     *
     * @param pk 删除数据的主键id
     * @return 处理后的vo对象
     */
    @Override
    public Boolean afterDelete(Long pk) {
        var flag = super.afterDelete(pk);
        // 设置权限的全局信息
        permissionCacheUtil.setGlobalTimestamp();
        return flag;
    }

    /**
     * 根据菜单id禁用菜单
     *
     * @param menuId  菜单id
     * @param disable 禁用状态 0-启用 1-禁用
     */
    @Override
    public void updateDisableById(Long menuId, Integer disable) {
        var po = getPoById(menuId);
        po.setIsDisable(disable == null ? ((po.getIsDisable() + 1) % 2) : (disable % 2));
        systemMenuMapper.updateById(po);
        // 设置权限的全局信息
        permissionCacheUtil.setGlobalTimestamp();
    }

    /**
     * 根据菜单id禁用菜单
     *
     * @param menuId 菜单id
     * @param show   禁用状态 0-显示 1-隐藏
     */
    @Override
    public void updateShowById(Long menuId, Integer show) {
        var po = getPoById(menuId);
        po.setIsShow(show == null ? ((po.getIsShow() + 1) % 2) : (show % 2));
        systemMenuMapper.updateById(po);
        // 设置权限的全局信息
        permissionCacheUtil.setGlobalTimestamp();
    }

    /**
     * 根据模块名称查询菜单
     *
     * @param module 模块名称
     * @param client 客户端
     * @return 菜单
     */
    @Override
    public SystemMenu getByModule(String module, String client) {
        var wrapper = lambdaQueryWrapper()
                .eq(SystemMenu::getCode, module)
                .eq(SystemMenu::getClientId, client)
                .eq(SystemMenu::getIsDisable, 0);
        return getOneByWrapper(wrapper);
    }

    /**
     * 高级排序
     *
     * @param form form
     */
    @Override
    public void sort(SystemMenuAdvancedSortForm form) {
        form.getItemList().forEach(item -> {
            systemMenuMapper.updateSortById(item.getId(), item.getSort());
        });
    }

}
